// PGERateCrap.cpp : Defines the entry point for the console application.
//

#include <Windows.h>
#include <StdIO.h>
#include "Usage.h"

#define TIER_USAGE	8.82
#define TIER_COUNT	5
#define INFINITE_KW	10000000.0

int iBillingPeriod = 32;

double DailyTotal( double* usage )
{
	double ret = 0;
	for( int i=0; i<HOURS_IN_DAY; i++ ) ret = ret + usage[ i ];
	return ret;
}

double CurrentE9( double* usage )
{
	double total = 0;
	double cost = 0;
	int TOURateWeek[ HOURS_IN_DAY ] = { 0,0,0,0,0,0,0,1,1,1,1,1, 1,1,2,2,2,2,2,2,2,1,1,1 };
	int TOURateWEnd[ HOURS_IN_DAY ] = { 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,1,1,1,1,0,0,0 };

	double tierUsage[ ] =	{ TIER_USAGE,		TIER_USAGE * 1.3,	TIER_USAGE * 2.0,	TIER_USAGE * 3.0,	INFINITE_KW };
	double tierRate[3][5] =	{{ 0.05294,			0.05294,			0.16200,			0.20200,			0.20200 },
							 { 0.11135, 		0.11135, 			0.30047, 			0.34047, 			0.34047 },
							 { 0.30470, 		0.30470, 			0.49382, 			0.53382, 			0.53382 }};

	for( int t=0; t<TIER_COUNT; t++ ) tierUsage[ t ] = tierUsage[ t ] * iBillingPeriod;

	double DailyCharge = 0.21888 + 0.14784;

	int dayOfWeek = 0;
	for( int d=0; d<iBillingPeriod; d++ )
	{
		for( int h=0; h<HOURS_IN_DAY; h++ ) 
		{
			total = total + usage[ h ];

			int t = 0;
			for( t=0; t<TIER_COUNT; t++ )
			{
				if( total < tierUsage[ t ] ) break;
			}

			double rate = 0;
			if(( dayOfWeek++ % 7 ) < 2 )
			{
				 rate = tierRate[TOURateWEnd[ h ]][ t ];
			}
			else
			{
				rate = tierRate[ TOURateWeek[ h ]][ t ];
			}

			cost = cost + usage[ h ] * rate;
		}
		
		cost = cost + DailyCharge;
	}

	return cost;
}




double NewE9( double* usage )
{
	double cost = 0;
	int TOURateWeek[ HOURS_IN_DAY ] = { 0,0,0,0,0,0,0,1,1,1,1,1, 1,1,2,2,2,2,2,2,2,1,1,0 };
	int TOURateWEnd[ HOURS_IN_DAY ] = { 0,0,0,0,0,0,0,1,1,1,1,1, 1,1,1,2,2,2,2,2,1,1,1,0 };

	double TOURateSummer[ ] = { 0.11041, 0.21431, 0.38451 };
	double TOURateWinter[ ] = { 0.11305, 0.17365, 0.27932 };

	double DailyCharge = 0.26283;

	int dayOfWeek = 0;
	for( int d=0; d<iBillingPeriod; d++ )
	{
		for( int h=0; h<HOURS_IN_DAY; h++ ) 
		{
			double rate = 0;
			if(( dayOfWeek++ % 7 ) < 2 )
			{
				 rate = TOURateSummer[ TOURateWEnd[ h ]];
			}
			else
			{
				rate = TOURateSummer[ TOURateWeek[ h ]];
			}
			cost = cost + usage[ h ] * rate;
		}
		cost = cost + DailyCharge;
	}

	return cost;
}

double E1( double* usage )
{
	double total = 0;
	double cost = 0;
	double tierUsage[ ] =	{ TIER_USAGE,		TIER_USAGE * 1.3,	TIER_USAGE * 2.0,	TIER_USAGE * 3.0,	INFINITE };
	double tierRate[ ] =	{ 0.12,				0.14,				0.30,				0.34,				0.34 };

	for( int t=0; t<TIER_COUNT; t++ ) tierUsage[ t ] = tierUsage[ t ] * iBillingPeriod;

	for( int d=0; d<iBillingPeriod; d++ )
	{
		for( int h=0; h<HOURS_IN_DAY; h++ )
		{
			total = total + usage[ h ];
			for( int i=0; i<TIER_COUNT; i++ )
			{
				if( total < tierUsage[ i ] )
				{
					cost = cost + usage[ h ] * tierRate[ i ];
					break;
				}
			}
		}
	}
	return cost;
}

void TestUsage( double *usage )
{
	printf( "Daily usage: % 7.2f \r\n", DailyTotal( usage ));
	printf( "E1         : % 7.2f \r\n", E1( usage ));
	printf( "Current E9 : % 7.2f \r\n", CurrentE9( usage )); 
	printf( "New E9     : % 7.2f \r\n", NewE9( usage )); 

}


int wmain(int argc, WCHAR* argv[])
{
	printf( "\r\nMy current usage: \r\n" );
	TestUsage( g_dailyUsageWithEV );

	printf( "\r\nEavy EV usage: \r\n" );
	TestUsage( g_dailyUsageHeavyEV );

	printf( "\r\nEavy Home usage: \r\n" );
	TestUsage( g_dailyUsageHeavyHome );
	
	return 0;
}

